#include "SysState.h"

using namespace std;

double sjf(vector<bool>& soln, const SysState& state ){
	//prepare soln
	soln.clear();
	soln = vector<bool>(state.n,0);

	double min_time = 100000; //arbitary large number
	int min_i = 0;
	int min_j = 0;
	for (int i = 0; i<state.n; ++i) {
		for (int j=i; j<state.n; ++j) {
		/*	if (state.B[i][j] != 0) {
				double avg_exe_time;
				if (i == j) {
					//no need to scan two files
					avg_exe_time = (state.Ts[i] + state.B[i][i]*state.Tn[i][i]) / state.B[i][i]; 
				} else {
					avg_exe_time = (state.Ts[i] + state.Ts[j] + state.B[i][j]*state.Tn[i][j] + state.B[i][i]*state.Tn[i][i] + state.B[j][j]*state.Tn[j][j]) / (state.B[i][i] + state.B[i][j] + state.B[j][j]);
				}
				if(avg_exe_time < min_time){
					min_time = avg_exe_time;
					min_i = i;
					min_j = j;
				}
			}*/ 
      if (state.B[i][j] != 0) {
        double avg_exe_time = 0;
        if (i == j) {
          avg_exe_time = (state.Ts[i] + state.B[i][i] * state.Tn[i][i]) / state.B[i][i];
		  
        } else {
          avg_exe_time = (state.Ts[i] + state.Ts[j] + state.B[i][j] * state.Tn[i][j]) / state.B[i][j];
        }
        if (avg_exe_time < min_time) {
          min_time = avg_exe_time;
          min_i = i;
          min_j = j;
        }
      }
    }
	}

	if(min_time == 100000){
		cout << "SJF doomed" << endl;
	}

	//return thingys
	soln[min_i] = true;
	soln[min_j] = true;
	return min_time*state.B[min_i][min_j];
}
